package com.xsomnus.network_programming.netty.white;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Date;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/4 0004 14:55
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 * <p>
 * 基于protobuf+netty的通信
 */
public class NettyProtobufServer {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ServerHandler1());
                    }
                });

        serverBootstrap.bind(9091).addListener(future -> {
           if (future.isSuccess()) {
               System.out.println(new Date() + ": 端口[9091]绑定成功!");
           } else {
               System.out.println("端口9091绑定失败");
           }
        });

    }

}
